invertRIconfInt <- function(dat, outcome.var, tr.var, tau.abs.min = -1, tau.abs.max = 1, tau.length = 10, n.sb.p = 100){
  
  taus <- seq(tau.abs.min, tau.abs.max, length = tau.length)
  ci95 <- ci90 <- ci80 <- array(NA)

  for(i in 1:length(taus)){
    
    tau <- taus[i]
    y <- dat[, outcome.var] 
    t <- dat[, tr.var]	
    
    ## given dat X/Y/Tr, calc W_{\tau_0}^D(T): (see hoima06)
    wdt <- sum(t*y)/sum(t) - sum((1-t)*y)/sum(1-t)
    
    wdT.dist <- array(NA)
    
    for(j in 1:n.sb.p){
      
      ## given dat X, do an SB.      
      sb1 <- seqblock1(id.vars = "id", id.vals = dat[1, "id"], exact.vars = c("gender", "phq1mdddx"), exact.vals = dat[1, c("gender", "phq1mdddx")], covar.vars = c("pssi1_tot", "age", "phq1tot", "fas1tot", "stroopINTtscore1", "lm1_t1recallSCALED", "lm2_t1recallSCALED"), covar.vals = dat[1, c("pssi1_tot", "age", "phq1tot", "fas1tot", "stroopINTtscore1", "lm1_t1recallSCALED", "lm2_t1recallSCALED")], tr.names = c("anagrams", "memory"), assg.prob.kfac = 5, file.name = "~/Desktop/sbout.RData", verbose = FALSE)
  
      for(n.idx in 2:nrow(dat)){
        sb2k <- seqblock2k(object = "~/Desktop/sbout.RData", id.vals = dat[n.idx, "id"], exact.vals = dat[n.idx, c("gender", "phq1mdddx")], covar.vals = dat[n.idx, c("pssi1_tot", "age", "phq1tot", "fas1tot", "stroopINTtscore1", "lm1_t1recallSCALED", "lm2_t1recallSCALED")], file.name = "~/Desktop/sbout.RData", verbose = FALSE)
      }
        
      t.tmp <- sb2k$orig[, "Tr"]
      t.tmp <- as.numeric(as.factor(t.tmp))-1
      
      ## t.tmp is "this rerandomized t".  t is the original randomization.
      wdT.dist[j] <- sum(t.tmp*(y + (1-t)*tau))/sum(t.tmp) - sum((1-t.tmp)*(y - t*tau))/sum(1-t.tmp)
    }
    
    if(.025 <= mean(wdT.dist >= wdt) & mean(wdT.dist >= wdt) <= .975){
      ci95 <- append(ci95, tau)
    }
    if(.05 <= mean(wdT.dist >= wdt) & mean(wdT.dist >= wdt) <= .95){
      ci90 <- append(ci90, tau)
    }
    if(.1 <= mean(wdT.dist >= wdt) & mean(wdT.dist >= wdt) <= .9){
      ci80 <- append(ci80, tau)
    }
    
  }
  
  ci95 <- ci95[2:length(ci95)]
  ci90 <- ci90[2:length(ci90)]
  ci80 <- ci80[2:length(ci80)]
  
  return(list(ci95=ci95, ci90=ci90, ci80=ci80))
  
}
